package com.ujiuye.Servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ujiuye.entity.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;


@WebServlet("/jsonServlet")
public class JsonServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //对象类型
        //Student student=new Student(1,"mary");

        //数组类型
        //String[] strs={"abc","bcd","cde"};


        //集合
        Student student1=new Student(1,"mary");
        Student student2=new Student(2,"jack");
        Student student3=new Student(3,"tom");
        List<Student> list=new ArrayList<>();
        list.add(student1);
        list.add(student2);
        list.add(student3);
        ObjectMapper mapper=new ObjectMapper();
        String json=mapper.writeValueAsString(list);
        System.out.println(json);
        PrintWriter out=response.getWriter();
        out.print(json);
        out.close();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
